/*********************************************************************
 *  ____                      _____      _                           *
 * / ___|  ___  _ __  _   _  | ____|_ __(_) ___ ___ ___  ___  _ __   *
 * \___ \ / _ \| '_ \| | | | |  _| | '__| |/ __/ __/ __|/ _ \| '_ \  *
 *  ___) | (_) | | | | |_| | | |___| |  | | (__\__ \__ \ (_) | | | | *
 * |____/ \___/|_| |_|\__, | |_____|_|  |_|\___|___/___/\___/|_| |_| *
 *                    |___/                                          *
 *                                                                   *
 *********************************************************************
 * Copyright 2010 Sony Ericsson Mobile Communications AB.         *
 * All rights, including trade secret rights, reserved.              *
 *********************************************************************/

/**
 * @file SelfRegistrationSmsUtils.java
 *
 * Utility functions for sending a self registration SMS.
 * This class increases the testability by separating the SMS functionality from
 * the SendSelfRegistrationSmsService.
 *
 * @author Magnus Fors (magnus.fors@sonyericsson.com)
 */

package com.moogene.saad.sns.provider;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.util.Log;

import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;

public class RegistrationUtils {

    /** Context of the service using this utility */
    private final Context mContext;

    /** format of SMS. */
    private static final String SMS_FORMAT = "IMEI:{0}/{1}/{2}/{3}/{4}";

    /** the CHARSET encoding */
    private static final String CHARSET = "UTF-8";


    /**
     * Constructor
     *
     * @param mContext the context of the calling service
     */
    public RegistrationUtils(Context context) {
        mContext = context;
    }


    /**
     * Create Message of SMS.
     *
     * @return Message of SMS
     */
    public String getRegMessageContent() {
        TelephonyManager tel = (TelephonyManager)mContext
                .getSystemService(Context.TELEPHONY_SERVICE);
        if (tel == null) {
            return null;
        }

        String selfPhoneNumber = tel.getLine1Number();
        String imei = tel.getDeviceId(); // IMEI value.
        String model = android.os.Build.MODEL;
        String manufacturer = android.os.Build.MANUFACTURER;
        String softver = android.os.Build.ID;
        if (selfPhoneNumber == null || imei == null ) {
            return null;
        }

        // define a format of SMS.
        MessageFormat msgFormat = new MessageFormat(SMS_FORMAT);
        // define contents of SMS.
        Object[] arg = {
                imei, selfPhoneNumber, manufacturer, model, softver
        };
        // set contents to format, and create message of SMS.
        String smsMsg = msgFormat.format(arg);
        return smsMsg;
    }
}
